﻿/**********************************天使物流*******************************************
* 功能描述:结算管理-付款管理
* 作者:石锦涛
* 时间:2022-01-12
* 
**********************************天使物流*******************************************/
using Snowflake.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TianShiProject.EntityFrameworkCore;
using TianShiProject.Models.Common;
using TianShiProject.Models.SettleAccounts.PaymentApplys;
using Volo.Abp.EntityFrameworkCore;

namespace TianShiProject.SettleAccounts.PaymentApplys
{
    public class PaymentApplyReposiyory : DapperDbContextRepository, IPaymentApplyReposiyory
    {
        public PaymentApplyReposiyory(IDbContextProvider<TianShiProjectDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        /// <summary>
        /// 付款分页显示
        /// </summary>
        /// <returns></returns>
        public async Task<PageResponse<PaymentApplyDto>> GetPayment(string tilet, string times, int? state, string name, int pageindex = 1, int pagesize = 5)
        {
            try
            {
                var sql = "select Id as GId,ApplyNumber,Headline,PaymentAmount,ClearingForm,ObjectOfPayment,OpeningBank,BankAccount,SUBSTRING(CONVERT(Nvarchar(50),DateOfPayment),1,10) as DateOfPayment,UserDescribe,Remarkm,Accessory,Proposer,SUBSTRING(CONVERT(Nvarchar(50), EstablishTimez), 1, 10) as EstablishTimez,ApprovalStatusj,Approverg,IsDelete from PaymentApply where IsDelete = 0   ";
                if (!string.IsNullOrEmpty(tilet))
                {
                    sql += $" and Headline like '%{tilet}%'";
                }
                if (!string.IsNullOrEmpty(times))
                {
                    sql += $" and DateOfPayment = '{times}'";
                }
                if (state > 0)
                {
                    sql += $" and ApprovalStatusj = '{state}'";
                }
                if (!string.IsNullOrEmpty(name))
                {
                    sql += $" and Approverg like '%{name}%'";
                }
                PageResponse<PaymentApplyDto> doc = new PageResponse<PaymentApplyDto>();
                var list = await this.QueryAsync<PaymentApplyDto>(sql, null);
                doc.PageTotal = list.Count();
                sql += $" order by Id offset ({pageindex}-1)*{pagesize} rows fetch next {pagesize} rows only";
                var query = await this.QueryAsync<PaymentApplyDto>(sql, null);
                doc.PageData = query;
                return doc;
            }
            catch (Exception)
            {

                throw;
            }
         }

        /// <summary>
        ///  付款逻辑删除
        /// </summary>
        /// <returns></returns>
        public async Task<PageResponse<PaymentApplyDto>> DeletePayment(int Id)
        {
            try
            {
                PageResponse<PaymentApplyDto> doc = new PageResponse<PaymentApplyDto>();
                var sql = $"update  PaymentApply set IsDelete=1 where Id={Id}";
                doc.Code = await this.ExecuteAsync(sql, null);
                return doc;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改付款
        /// </summary>
        /// <param name="newInput"></param>
        /// <returns></returns>
        public async Task<PageResponse<int>> UpdatePayment(PaymentApplyDto applyDto)
        {
            try
            {
                var sql = $" update PaymentApply set ApplyNumber='{applyDto.ApplyNumber}',Headline='{applyDto.Headline}',PaymentAmount='{applyDto.PaymentAmount}',ClearingForm='{applyDto.ClearingForm}',ObjectOfPayment='{applyDto.ObjectOfPayment}',OpeningBank = '{applyDto.OpeningBank}',BankAccount = '{applyDto.BankAccount}',DateOfPayment = '{applyDto.DateOfPayment.ToString("yyyy/MM/dd")}',UserDescribe = '{applyDto.UserDescribe}',Remarkm = '{applyDto.Remarkm}',Accessory = '{applyDto.Accessory}',Proposer = '{applyDto.Proposer}',EstablishTimez = '{applyDto.EstablishTimez.ToString("yyyy/MM/dd")}',ApprovalStatusj = '{applyDto.ApprovalStatusj}',Approverg = '{applyDto.Approverg}',IsDelete = '{applyDto.IsDelete}'  where Id = '{applyDto.GId}'";
                PageResponse<int> doc = new PageResponse<int>();

                doc.Code = await this.ExecuteAsync(sql, null);
                return doc;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 新增付款
        /// </summary>
        /// <param name="newInput"></param>
        /// <returns></returns>
        public async Task<PageResponse<int>> PostPayment(PaymentApplyDto applyDto)
        {
            try
            {
                //雪花算法
                var worker = new IdWorker(1, 1);
                var id = worker.NextId().ToString();

                var sql = $"  insert PaymentApply values ('{id}','{applyDto.Headline}','{applyDto.PaymentAmount}','{applyDto.ClearingForm}','{applyDto.ObjectOfPayment}','{applyDto.OpeningBank}','{applyDto.BankAccount}','{applyDto.DateOfPayment.ToString("yyyy/MM/dd")}','{applyDto.UserDescribe}','{applyDto.Remarkm}','{applyDto.Accessory}','{applyDto.Proposer}',GETDATE(),'{applyDto.ApprovalStatusj}','{applyDto.Approverg}','{applyDto.IsDelete}') ";
                PageResponse<int> doc = new PageResponse<int>();
                doc.Code = await this.ExecuteAsync(sql, null);
                return doc;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 批量逻辑删除付款
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<PageResponse<PaymentApplyDto>> DeletePaymentMent(string Id)
        {
            try
            {
                PageResponse<PaymentApplyDto> doc = new PageResponse<PaymentApplyDto>();
                var ids = Id.Split(',');
                foreach (var item in ids)
                {
                    var shu = Convert.ToInt32(item);
                    var list = await this.QueryAsync<PaymentApplyDto>($"select * from PaymentApply where Id={shu}");
                    if (list != null)
                    {
                        var sql = $"update PaymentApply set IsDelete=1 where Id={shu} ";
                        doc.Code = await this.ExecuteAsync(sql, null);
                    }
                }
                return doc;
            }
            catch (Exception)
            {

                throw;
            }
        }

    }
}
